#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
from collections import defaultdict
from pathlib import Path

import xlrd


class ParseExcel(object):
    def __init__(self, file_path):
        if Path.exists(file_path):
            self.excel = file_path
        else:
            raise FileNotFoundError(f'{file_path} is not exits')

    def _read_excel(self):
        """
        Read excel file
        :return: xml_data.json
        """
        excel_data = xlrd.open_workbook(self.excel)
        return excel_data

    def get_sheet(self, sheet_num=None, sheet_name=None):
        """
        Get excel all sheets or specify sheet
        :param sheet_name: sheet name
        :param sheet_num:  sheet num
        :return: sheets
        """
        data = self._read_excel()
        if isinstance(sheet_num, int):
            return data.sheet_by_index(sheet_num)
        if isinstance(sheet_name, str):
            return data.sheet_by_name(sheet_name)
        return [data.sheet_by_name(name) for name in data.sheet_names()]

    def get_cell_data(self, rows: list, cols: list, sheet_num=None, sheet_name=None):
        """
        Get specify sheet_num,sheet_name,rows,cols xml_data.json
        :param rows:
        :param cols:
        :param sheet_num:
        :param sheet_name:
        :return:
        """
        excel_sheets = self.get_sheet(sheet_num, sheet_name)
        response = list()
        if isinstance(excel_sheets, list):
            for excel_sheet in excel_sheets:
                self._get_value(cols, excel_sheet, rows, response)
        else:
            self._get_value(cols, excel_sheets, rows, response)
        return response

    @staticmethod
    def _get_value(cols, excel_sheet, rows, response):
        """
        Get value
        :param cols:
        :param excel_sheet:
        :param rows:
        :param response:
        :return:
        """
        if not rows:
            rows = range(1, excel_sheet.nrows)
        if not cols:
            cols = range(1, excel_sheet.ncols)
        sheet_response = list()
        for row in rows:
            for col in cols:
                sheet_response.append({excel_sheet.cell_value(0, col): excel_sheet.cell_value(row, col)})
        response.append(sheet_response)


if __name__ == '__main__':
    excel_file = Path(Path.cwd(), "cve.xlsx")
    parse_excel = ParseExcel(excel_file)
    print(parse_excel.get_cell_data(rows=[], cols=[2, 5]))
